package com.meilai.project.exception;

import com.meilai.project.dto.CommonResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Log4j2
public class CommonExceptionHandler {

    @ExceptionHandler(CommonException.class)
    public CommonResult handleException(CommonException e){
        log.error(e.getMessage(),e);
        if ( e.getCode() == null ) {
            return new CommonResult().failed(e.getMessage());
        } else {
            return new CommonResult().failed(e.getMessage(), e.getCode());
        }
    }
}
